/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vu.demo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import net.vu.demo.bean.Employee;
import net.vu.demo.bean.EmployeeService;

/**
 *
 * @author VUHOLMES
 */
public class EmployeeAction extends ActionSupport implements ModelDriven, Preparable {

    private static final long serialVersionUID = 1L;
    private int id; 
    private Employee employee;
    private EmployeeService service = new EmployeeService();
    
    public int getId() { 
        return id; 
    }
    
    public void setId(int id) { 
        this.id = id; 
    }
    
    /* 
    * This method is call automatically when the Class is called. 
    * 
    */

    public Object getModel() {
        System.out.println("getModel() in Employee Action"); 
        return employee;
    }

    public void prepare() throws Exception {
        System.out.println("prepare() in Employee Action"); 
        System.out.println("EmployeeID = "+id); 
        if (id==0){
            employee = new Employee(); 
            System.out.println("Tao moi ");
        }
        else { 
            employee = service.findById(id); 
            System.out.println("Tim "); 
        }
    }
    
    public String addEmployee(){ 
        System.out.println("addEmployee() in Employee Action"); 
        service.addEmployee(employee); 
        return SUCCESS; 
    } 
    public String deleteEmployee(){ 
        System.out.println("deleteEmployee() in Employee Action"); 
        System.out.println(employee.getId()); 
        service.deleteEmployee(employee.getId()); 
        return SUCCESS; 
    }
    
    public String updateEmployee(){ 
        System.out.println("updateEmployee() in Employee Action"); 
        service.updateEmployee(employee); 
        return SUCCESS;
    }
    
    @Override
    public String execute() throws Exception{ 
        System.out.println("execute() in Employee Action"); 
        return SUCCESS; 
    } 
    @Override
    public String input() throws Exception{ 
        System.out.println("input() in Employee Action"); 
        return INPUT; 
    }
}
